﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace doxyOutParser.CodeCheck
{
	public class TIIncludeDirsParser : IIncludeDirsParser
	{
		private string _tiProjectFile;
		public TIIncludeDirsParser(string tiProjectFile)
		{
			//TODO Check the value of string and eventually check if exist or not.
			this._tiProjectFile = tiProjectFile;
		}


		#region IIncludeDirsParser Members

		public string[] GetIncludeDirsTable()
		{
			// Using of StreamReader to parse the file
			StreamReader tiProjectStreamReader = new StreamReader(this._tiProjectFile);
			string line = tiProjectStreamReader.ReadLine();
			string[] dirsSeparators = { ";" };

			List<string> includeDirsTempList = new List<string>();

			while (line != null)
			{
				if (line.Contains("-i\""))
				{
					line = line.Substring(line.IndexOf("-i\""));
					do
					{
						int startIndex = line.IndexOf("-i\"");
						
						int endIndex = line.IndexOf("\"",startIndex+3);
						if (endIndex != -1 && endIndex > startIndex)
						{
							string includeDir = line.Substring(startIndex + 3, (endIndex - startIndex-3));
							includeDirsTempList.Add(includeDir);
						}
						line = line.Substring(endIndex+1);
					} while (line.Contains("-i\""));
					return includeDirsTempList.ToArray();
				}
				// Read Next Line
				line = tiProjectStreamReader.ReadLine();
			}
			return includeDirsTempList.ToArray();
		}

		#endregion

	}
}
